package com.xncoding.jwt.config;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;
import javax.websocket.server.ServerEndpointConfig.Configurator;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebSocketConfig extends Configurator {
	@Bean
	public ServerEndpointExporter serverEndpointExporter() {
		return new ServerEndpointExporter();
	}

	@Override 
    public void modifyHandshake(ServerEndpointConfig config, 
    		HandshakeRequest request, 
    		HandshakeResponse response) {
		HttpSession httpSession = (HttpSession) request.getHttpSession();
    	if(httpSession != null){ 
    		System.out.println("websocket用户"+httpSession.getAttribute("username"));
    		config.getUserProperties().put("username", httpSession.getAttribute("username"));
    	}
    }

}